home *** CD-ROM | disk | FTP | other *** search
- Path: isonews.bbn.hp.com!hpbblb!news
- From: Matthias Dittrich <matti>
- Newsgroups: comp.lang.c
- Subject: Re: How to make this get_time function work ?
- Date: 6 Mar 1996 17:31:04 GMT
- Organization: Hewlett-Packard Co.
- Message-ID: <4hki4o$1ju@hpbblb.bbn.hp.com>
- References: <4hhjct$3og@idefix.eunet.fi>
- NNTP-Posting-Host: trabant.bbn.hp.com
- Mime-Version: 1.0
- Content-Type: text/plain; charset=us-ascii
- Content-Transfer-Encoding: 7bit
- X-Mailer: Mozilla 1.1N (X11; I; HP-UX A.09.07 9000/712)
- X-URL: news:4hhjct$3og@idefix.eunet.fi
-
- mtg@neste.com (Michael Glasgow) wrote:
- >#include <stdio.h>
- >#include <ctype.h>
- >#include <time.h>
- >#include <math.h>
- >#include <string.h>
- >#define STRING_LENGTH 256
- >
- >int get_time() {
- If you are returning a pointer to char, then you should declare this:
- char* get_time()...
-
- > time_t t=time(NULL);
- > struct tm *tp=localtime(&t);
- > char time_str[STRING_LENGTH]={0}, str[STRING_LENGTH]={0};
- This is a critical point. With return str you are returning a pointer to
- the stack, which in most cases not contains what you are expecting. Declare
- str as static or return allocated memory for instance.
- Otherwise it's possible that your program writes a big file named core.
-
- > strftime(time_str, STRING_LENGTH, "%Y%m%d%H%M%S", tp);
- > sprintf(str,"%s",time_str);
- >/* printf("%s",str); */
- > return str;
- >}
- >main() {
- > int tim;
- This also must be: char* tim;
-
- > tim = get_time();
- > printf("%s\n", tim);
- >}
- >
- Good luck,
- Matthias
-
-